/*	kd:
	The draw cache is not really an essential component of the shop, but it does
	reduce checking the user's inventory close to minimum. It has to provide the
	following output for each item:
	
	1. Amount and maximum in player's inventory
	2. Lit lamp count
	3. Lamp colour
	4. Icon
	5. Darkening + equip light up
	
	2 + 3 is just the background button sprite here.
	
	The idea is not just to use the fact the player's inventory rarely updates,
	but also to keep all polling related code out of the RenderOverlay calls -
	Because zs won't even allow it.
	
	This essentially forces you to adopt an event-driven design in the shop,
	but that's no problem - Even if the shop were an acs dialogue or so... */

struct Gcs_DrawCache play {
	uint m_size;
	
	// How many lamps in total on the button sprites.
	const lamp_count = 8;
	
	Array <int>			m_amount;
	Array <int>			m_max_amount;
	Array <TextureId>	m_button_sprite;
	Array <TextureId>	m_highlighted_sprite;
	Array <TextureId>	m_button_icon;
	Array <double>		m_darken;
	Array <double>		m_equip_alpha;
	double				m_scale;
	
	TextureId LampSprite (uint i) const {
		return m_button_sprite [i];
	}
	
	TextureId HighlightSprite (uint i) const {
		return m_highlighted_sprite [i];
	}
	
	TextureId ItemSprite (uint i) const {
		return m_button_icon [i];
	}
	
	double DarkenAlpha (uint i) const {
		return m_darken [i];
	}
	
	double EquipAlpha (uint i) const {
		return m_equip_alpha [i];
	}
	
	// Scaling fall-off to prevent overlap with many sprites.
	const max_scale_count       = 6;
	const min_scale_count       = 20;
	const max_button_scale      = 1.0f;
	const min_button_scale      = 0.65f;
	
	// zs has no default struct assignment, yet.
	void CopyTo (Gcs_DrawCache cache) {
		cache.ClearAllReserve(m_size);
		
		if(0 < m_size) {
			cache.m_amount.              Copy(m_amount);
			cache.m_max_amount.          Copy(m_max_amount);
			cache.m_button_sprite.       Copy(m_button_sprite);
			cache.m_highlighted_sprite.  Copy(m_highlighted_sprite);
			cache.m_button_icon.         Copy(m_button_icon);
			cache.m_darken.              Copy(m_darken);
			cache.m_equip_alpha.         Copy(m_equip_alpha);
			cache.m_size   = m_size;
			cache.m_scale  = m_scale;
		}
	}
	
	void ClearAllReserve (uint size) {
		m_amount.               Clear();
		m_max_amount.           Clear();
		m_button_sprite.        Clear();
		m_highlighted_sprite.   Clear();
		m_button_icon.          Clear();
		m_darken.               Clear();
		m_equip_alpha.          Clear();
		
		m_amount.               Reserve(size);
		m_max_amount.           Reserve(size);
		m_button_sprite.        Reserve(size);
		m_highlighted_sprite.   Reserve(size);
		m_button_icon.          Reserve(size);
		m_darken.               Reserve(size);
		m_equip_alpha.          Reserve(size);
		
		m_size = size;
		
		// m_size is exactly the item count. Since we don't cache without
		// reserving, we already know the ideal scaling factor now.
		
		int d_count = min_scale_count - max_scale_count;
		
		m_scale = clamp((
			1.0 * (size - max_scale_count) * min_button_scale +
			1.0 * (min_scale_count - size) * max_button_scale) / d_count,
			min_button_scale,
			max_button_scale);
	}
	
	void FillFromInventory (PlayerInfo user, Gc_Shop_Inv inv,
		Gcs_ButtonSprites sprites) {
		if(!user || !inv) {
			return;
		}
		
		Actor mo = user.mo;
		
		for(uint i = 0; i < m_size; i++) {
			let  handler     = inv.m_type [i];
			let  inv_class   = inv.m_inv_class [i];
			int  amount      = 0;
			int  max_amount  = 0;
			int  lamps       = 0;
			uint afford      = inv.CanBuyXtimes(mo, i);
			bool worse       = false;
			bool marked      = false;
			bool equipped    = false;
			bool is_new      = false;
			
			if(handler) {
				is_new       = inv.m_new [i];
				worse        = handler.Worse(mo, inv_class);
				marked       = handler.IsMarked();
				amount       = handler.Count(mo, inv_class);
				max_amount   = handler.MaxInv(mo, inv_class);
				lamps        = clamp((float)(lamp_count * amount) / max_amount, 0, lamp_count);
				equipped     = handler.IsEquipped(mo, inv_class);
			}
			
			// Show the equip highlight, if indeed equipped.
			m_equip_alpha [i] = equipped ? sprites.equip_ring_alpha : 0;
			
			// Sprite selection is something weird.
			m_highlighted_sprite [i] = sprites.Highlight(lamps);
			
			if(is_new) {
				m_button_sprite [i] = sprites.Gold(lamps);
			}
			
			else if(worse) {
				m_button_sprite [i] = sprites.Worse(lamps);
			}
			
			else if(marked) {
				m_button_sprite [i] = sprites.Shiny(lamps);
			}
			
			else {
				m_button_sprite [i] = sprites.Normal(lamps);
			}
			
			String icon_lump = StringTable.Localize(
				String.Format("$ICON_%s", inv.m_inv_resource_key [i]));
			
			m_button_icon   [i] = TexMan.CheckForTexture(icon_lump, TexMan.TYPE_ANY);
			m_darken        [i] = (afford == 0 && !sv_infiniteinventory) ?
				sprites.darken_amount : 0;
		}
	}
	
	// Version for quick select that doesn't need a Gc_Shop_Inv. Don't bother
	// splitting this apart to avoid repeat code.
	void FillForQuickSelect (
		PlayerInfo				user,
		Gcq_SelectableItems		inv,
		Gcs_ButtonSprites		sprites) {
		if(!user || !inv) {
			return;
		}
		
		Actor mo = user.mo;
		
		for(uint i = 0; i < m_size; i++) {
			let  inv_class	= inv.InventoryType(i);
			let  handler	= inv.HandlerInst(i);
			int  amount		= 0;
			int  max_amount	= 0;
			int  lamps		= 0;
			bool worse		= false;
			bool marked		= false;
			bool equipped	= false;
			bool is_new		= false;
			
			if(handler) {
				worse        = handler.Worse(mo, inv_class);
				marked       = handler.IsMarked();
				amount       = handler.Count(mo, inv_class);
				max_amount   = handler.MaxInv(mo, inv_class);
				lamps        = clamp((float)(lamp_count * amount) / max_amount, 0, lamp_count);
				equipped     = handler.IsEquipped(mo, inv_class);
			}
			
			// Show the equip highlight, if indeed equipped.
			m_equip_alpha [i] = equipped ? sprites.equip_ring_alpha : 0;
			
			// Sprite selection is something weird.
			m_highlighted_sprite [i] = sprites.Highlight(lamps);
			
			/* if(is_new) {
				m_button_sprite [i] = sprites.Gold(lamps);
			} */
			
			if(worse) {
				m_button_sprite [i] = sprites.Worse(lamps);
			}
			
			else if(marked) {
				m_button_sprite [i] = sprites.Shiny(lamps);
			}
			
			else {
				m_button_sprite [i] = sprites.Normal(lamps);
			}
			
			String icon_lump = StringTable.Localize(
				String.Format("$ICON_%s", inv.InventoryName(i)) );
			
			m_button_icon   [i] = TexMan.CheckForTexture(icon_lump, TexMan.TYPE_ANY);
			m_darken        [i] = 0;
		}
	}
}

/*	kd:
	This variant outputs information to smoothly change between button graphics
	which depend on your inventory. All it does is keep two snapshots of a
	draw cache. From here, we can go ahead and interpolate between them to make
	a smooth transition possible. */

struct Gcs_SmoothCache play {
	const transition_tics = 10;
	const alpha_rate      = 1.0 / transition_tics;
	
	Gcs_DrawCache    m_start;
	Gcs_DrawCache    m_end;
	Gcs_DrawCache    m_pending;
	uint             m_tics;
	
	float            m_start_alpha;
	float            m_end_alpha;
	bool             m_is_active;
	bool             m_has_pending_job;
	
	/*	The cache waits to complete its current interpolation job. If the user
		clicks something during that time, we of course don't want to suddenly
		change the "target" picture.
		
		So if that happens, the new-new picture gets stuffed into "pending", and
		we remember that this happened, appending another transition after the
		fact, so the user will always see a smooth transition that ends up with
		the most up to date inventory information. */
	
	void Interpolate () {
		if(!m_is_active) {
			return;
		}
		
		m_tics++;
		
		m_end_alpha     = m_tics * alpha_rate;
		m_start_alpha   = 1 - m_end_alpha;
		
		if(transition_tics <= m_tics) {
			m_is_active    = false;
			m_tics         = 0;
			
			m_end_alpha    = 1;
			m_start_alpha  = 0;
			m_end.CopyTo(m_start);
			
			if(m_has_pending_job) {
				m_pending.CopyTo(m_end);
				m_has_pending_job = false;
			}
		}
	}
	
	void TransitionTo (Gcs_DrawCache new_cache) {
		if(m_is_active) {
			m_has_pending_job = true;
			new_cache.CopyTo(m_pending);
		}
		
		else {
			m_has_pending_job = false;
			m_is_active     = true;
			m_tics          = 0;
			m_start_alpha   = 1;
			m_end_alpha     = 0;
			new_cache.CopyTo(m_end);
		}
	}
	
	void SetStartCache (Gcs_DrawCache cache) {
		cache.CopyTo(m_start);
		cache.CopyTo(m_end);
	}
}